package com.woniuxy.cloud.userassembly.apiimpl;


import com.woniuxy.cloud.demo.Result;
import com.woniuxy.cloud.dto.BaseDto;
import com.woniuxy.cloud.userassembly.dto.UsersSelRolesDto;
import com.woniuxy.cloud.userassembly.entity.Role;
import com.woniuxy.cloud.userassembly.exception.RoleException;
import com.woniuxy.cloud.userassembly.param.RolesPageAndByKeyParam;
import com.woniuxy.cloud.userassembly.param.SaveRoleParam;
import com.woniuxy.cloud.userassembly.param.UpdateRoleParam;
import com.woniuxy.cloud.userassembly.service.RoleService;
import com.woniuxy.cloud.userassembly.util.ValidatedUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 周川雄
 * @since 2022-01-26
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色接口")
public class RoleController {
    @Resource
    private RoleService rts;

    /**
     * 修改角色数据
     *
     * @param urp
     * @param result
     * @return
     */
    @ApiOperation("修改角色数据")
    @PostMapping("/updateRole.do")
    public Result updateUser(@RequestBody @Validated UpdateRoleParam urp, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);

        if (rts.updateRole(urp)) {
            return Result.newInstance().setCode(200).setMsg("修改成功");
        }
        throw new RoleException("修改角色失败", 501);
    }

    /**
     * 根据id查询用户
     *
     * @param roleId
     * @return
     * @throws Exception
     */
    @ApiOperation("根据id查询角色")
    @GetMapping("/findRoleById/{roleId}")
    @ApiImplicitParam(name = "roleId", value = "角色id")
    public Result findRoleById(@PathVariable Integer roleId) throws Exception {

        return Result.newInstance().setMsg("查询成功").setCode(200).setData(rts.findRoleById(roleId));
    }

    /**
     * 添加新角色
     *
     * @param srp
     * @param result
     * @return
     */
    @ApiOperation("添加新角色")
    @PostMapping("/SaveRole.do")
    public Result SaveRole(@RequestBody @Validated SaveRoleParam srp, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        if (rts.SaveRole(srp)) {
            return Result.newInstance().setCode(200).setMsg("添加成功");
        }
        throw new RoleException("添加新角色失败", 502);

    }


    /**
     * 分页和模糊查询所有角色
     *
     * @param rp
     * @return
     */
    @ApiOperation("分页和模糊查询所有角色")
    @PostMapping("listRoles.do")
    public Result listRoles(@RequestBody RolesPageAndByKeyParam rp) {
        BaseDto<Role> pageRoleDto = rts.listRoles(rp);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(pageRoleDto);
    }

    /**
     * 查询当前用户绑定的所有角色和勾选状态
     *
     * @param userId
     * @return
     */
    @ApiOperation("查询当前用户绑定的所有角色和勾选状态")
    @ApiImplicitParam(name = "userId", value = "用户id")
    @PostMapping("/listRolesByUserId.do/{userId}")
    public Result listRolesByUserId(@PathVariable Integer userId) {
        UsersSelRolesDto dto = rts.listRolesByUserId(userId);
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(dto);
    }


    /**
     * 批量删除角色(逻辑删除)
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除角色(逻辑删除)")
    @PostMapping("/removeRoleByIds.do")
    public Result removeRoleByIds(@RequestBody List<Integer> ids) {
        if (rts.removeUserByIds(ids)) {
            return Result.newInstance().setCode(200).setMsg("删除成功");
        }
        throw new RoleException("删除失败", 503);
    }
}

